/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tj.eskhata.pos.web.output;

import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import tj.eskhata.pos.common.WiaSession;
import tj.eskhata.pos.domain.Client;
import tj.eskhata.pos.domain.PsDoctrans;
import tj.eskhata.pos.domain.Sost;
import tj.eskhata.pos.domain.User;
import tj.eskhata.pos.services.PosService;


/**
 * implementation of IDataProvider for contacts that keeps track of sort information
 * 
 * @author igor
 * 
 */
public class PSDocDataProvider extends SortableDataProvider<PsDoctrans>
{
	@SpringBean
	private PosService service;
	private User user;
	/**
	 * constructor
	 */
	public PSDocDataProvider()
	{
		 InjectorHolder.getInjector().inject(this);	
	}
	
	public PSDocDataProvider( User user)
	{
		 InjectorHolder.getInjector().inject(this);
		 this.user=user;
	}
	
	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
	 */
	public Iterator<PsDoctrans> iterator(int first, int count)
	{
		
		
		Sost sost=service.findByCriteriaSost(Restrictions.eq("code", "INPUT_BY_OPER")).get(0);
		List<PsDoctrans> lst=service.findByCriteriaPsDoctrans(Restrictions.eq("createUser.id", user.getId()),Restrictions.eq("sost.id", sost.getId()));
		int n=lst.size()>30?30:lst.size();
		return lst.subList(first, first+count).iterator();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size()
	{	
		Sost sost=service.findByCriteriaSost(Restrictions.eq("code", "INPUT_BY_OPER")).get(0);
		int i=service.findByCriteriaPsDoctrans(Restrictions.eq("createUser.id", user.getId()),Restrictions.eq("sost.id", sost.getId())).size();
		return i<30?i:30;
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<PsDoctrans> model(final PsDoctrans object)
	{
		return new AbstractReadOnlyModel<PsDoctrans>() {
			@Override
			public PsDoctrans getObject() {
				return object;
			}
		};
		//return new DetachableContactModel(object);
	}

}
